$(function() {
	var pageSize = $("#pageSize").val();
	$.post("http://localhost:8081/study/two/produce/findAll", {
		pageNow: 1,
		pageSize: 10
	}, function(data) {
		console.log(data)
		var list = data.list;
		console.log(list);
		var innerHtml = "";
		$.each(list, function(index, data) {
			console.log(data)
			innerHtml += "<tr><td>" + data.proName + "</td><td class=\"center\">" + data.id + "</td><td class=\"center price\">" + data.proPrice + "</td><td class=\"center\"><input type=\"text\" class=\"span2 number\"/></td></tr>"
		});
		$("#listContent").html(innerHtml);
		var pageInfo = data.data;
		var pageInnerHtml = decoratePage(pageInfo);
		console.log(pageInnerHtml)
		$("#pageInfo").html(pageInnerHtml)
	})

	$("#count").click(function() {
		var priceList = $(".center.price");
		var numberList = $(".span2.number");
		console.log(priceList);
		console.log(numberList);
		var count = 0;
		for(let i = 0; i < priceList.length; i++) {
			if("" == numberList.eq(i).val()) {
				continue;
			}
			count += parseFloat(priceList.eq(i).text()) * parseFloat(numberList.eq(i).val());
		}
		console.log(count);
		$("#countResult").text(count);
	})

})

function turnTo(a) {
	var pageNow = $(a).text();
	var pageSize = $("#pageSize").val();
	$("#listContent").html("");
	$.post("http://localhost:8081/study/two/produce/findAll", {
		pageNow: pageNow,
		pageSize: pageSize
	}, function(data) {
		console.log(data)
		var list = data.list;
		console.log(list);
		var innerHtml = "";
		$.each(list, function(index, data) {
			console.log(data)
			innerHtml += "<tr><td>" + data.proName + "</td><td class=\"center\">" + data.id + "</td><td class=\"center price\">" + data.proPrice + "</td><td class=\"center\"><input type=\"text\" class=\"span2 number\"/></td></tr>"
		});
		$("#listContent").html(innerHtml);
		var pageInfo = data.data;
		var pageInnerHtml = decoratePage(pageInfo);
		console.log(pageInnerHtml)
		$("#pageInfo").html(pageInnerHtml)
	})
}

function changeSelect() {
	var pageNow = 1;
	var pageSize = $("#pageSize").val();
	$("#listContent").html("");
	$.post("http://localhost:8081/study/two/produce/findAll", {
		pageNow: pageNow,
		pageSize: pageSize
	}, function(data) {
		console.log(data)
		var list = data.list;
		console.log(list);
		var innerHtml = "";
		$.each(list, function(index, data) {
			console.log(data)
			innerHtml += "<tr><td>" + data.proName + "</td><td class=\"center\">" + data.id + "</td><td class=\"center price\">" + data.proPrice + "</td><td class=\"center\"><input type=\"text\" class=\"span2 number\"/></td></tr>"
		});
		$("#listContent").html(innerHtml);
		var pageInfo = data.data;
		var pageInnerHtml = decoratePage(pageInfo);
		console.log(pageInnerHtml)
		$("#pageInfo").html(pageInnerHtml)
	})
}
//前一页
function turnToPrev(a, b) {
	var now = a; //当前页
	var total = b; //总共的页数
	if(a == 1) {
		return;
	} else {
		var pageSize = $("#pageSize").val();
		$("#listContent").html("");
		$.post("http://localhost:8081/study/two/produce/findAll", {
			pageNow: (now-1),
			pageSize: pageSize
		}, function(data) {
			console.log(data)
			var list = data.list;
			console.log(list);
			var innerHtml = "";
			$.each(list, function(index, data) {
				console.log(data)
				innerHtml += "<tr><td>" + data.proName + "</td><td class=\"center\">" + data.id + "</td><td class=\"center price\">" + data.proPrice + "</td><td class=\"center\"><input type=\"text\" class=\"span2 number\"/></td></tr>"
			});
			$("#listContent").html(innerHtml);
			var pageInfo = data.data;
			var pageInnerHtml = decoratePage(pageInfo);
			console.log(pageInnerHtml)
			$("#pageInfo").html(pageInnerHtml)
		})
	}
}

//后一页
function turnToNext(a, b) {
	var now = a; //当前页
	var total = b; //总共的页数
	if(now == total) {
		return;
	} else {
		var pageSize = $("#pageSize").val();
		$("#listContent").html("");
		$.post("http://localhost:8081/study/two/produce/findAll", {
			pageNow: (parseInt(a)+1),
			pageSize: pageSize
		}, function(data) {
			console.log(data)
			var list = data.list;
			console.log(list);
			var innerHtml = "";
			$.each(list, function(index, data) {
				console.log(data)
				innerHtml += "<tr><td>" + data.proName + "</td><td class=\"center\">" + data.id + "</td><td class=\"center price\">" + data.proPrice + "</td><td class=\"center\"><input type=\"text\" class=\"span2 number\"/></td></tr>"
			});
			$("#listContent").html(innerHtml);
			var pageInfo = data.data;
			var pageInnerHtml = decoratePage(pageInfo);
			console.log(pageInnerHtml)
			$("#pageInfo").html(pageInnerHtml)
		})
	}
}
